using System;

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Magnet.Engine;
#endregion

namespace Magnet
{
    /// <summary>
    /// Sample showing how to manage different game states, with transitions
    /// between menu screens, a loading screen, the game itself, and a pause
    /// menu. This main game class is extremely simple: all the interesting
    /// stuff happens in the ScreenManager component.
    /// </summary>
    public class MagnetGame : Microsoft.Xna.Framework.Game
    {
        public static GraphicsDeviceManager Graphics
        {
            get { return graphics; }
        }

        private static GraphicsDeviceManager graphics;
        private ScreenManager screenManager;

        float timeToExit = 0f;

        /// <summary>
        /// The main game constructor.
        /// </summary>
        public MagnetGame()
        {
            // initialize the graphics system
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;

            TextureManager.Initialize(this);

            //Makes the game have variable FPS
            IsFixedTimeStep = false;

            // configure the content manager
            Content.RootDirectory = "Content";

            // add the screen manager
            screenManager = new ScreenManager(this);
            Components.Add(screenManager);
            
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to 
        /// before starting to run.  This is where it can query for any required 
        /// services and load any non-graphic related content.  Calling base.Initialize 
        /// will enumerate through any components and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // add the main menu screen.
            screenManager.AddScreen(new BackgroundScreen());
            screenManager.AddScreen(new MainMenuScreen());

            Sound.Initialize();

            Sound.ChangeMusic(Sound.MusicList.MusicIntro);
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            InputManager.Update();
            Sound.Update();

            float elapsedSeconds = (float)gameTime.ElapsedRealTime.Milliseconds / 1000f;

            ExitCheck(elapsedSeconds);

            base.Update(gameTime);
        }


        /// <summary>
        /// This checks to see if the game should do a quick exit, caused by holding the back button for 2 seconds.
        /// </summary>
        /// <param name="elapsedSeconds">Number of seconds passed since the last time update was called.</param>
        private void ExitCheck(float elapsedSeconds)
        {
            if (InputManager.IsKeyPressed(InputManager.BACK)
                || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                timeToExit += elapsedSeconds;
            else
            {
                if (timeToExit != 0f)
                    timeToExit = 0f;
            }

            if (timeToExit > 2f)
                Exit();
        }


        #region Drawing

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            // The real drawing happens inside the screen manager component.
            base.Draw(gameTime);
        }

        #endregion


        #region Entry Point


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (MagnetGame game = new MagnetGame())
            {
                game.Run();
            }
        }


        #endregion
    }
}

